<!-- 添加或者修改业绩信息 -->
<template>
  <div class="main-wrapper">
    <el-form ref="dataForm" :rules="rules" :model="achievementData" label-position="left" label-width="110px">
      <el-form-item label="项目名称">
        <el-input v-model="achievementData.name" placeholder="请填写项目名称" />
      </el-form-item>
      <el-form-item label="业主单位">
        <el-input v-model="achievementData.owner" placeholder="请填写业主单位" />
      </el-form-item>
      <el-form-item label="合同签订时间">
        <el-date-picker
          v-model="achievementData.contractTime"
          type="date"
          placeholder="请填写合同签订时间"
        />
      </el-form-item>
      <el-form-item label="投资额">
        <el-input v-model="achievementData.investMoney" type="number" placeholder="请填写投资额" />
      </el-form-item>
      <el-form-item label="合同金额">
        <el-input v-model="achievementData.contractMoney" type="number" placeholder="请填写合同金额" />
      </el-form-item>
      <el-form-item label="合同签署公司">
        <el-input v-model="achievementData.branchId" placeholder="请填写合同签署公司" />
      </el-form-item>
      <el-form-item label="项目类别">
        <el-select v-model="achievementData.busiId" placeholder="请选择类型">
          <el-option v-for="item in busiConfigs" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="专业">
        <el-cascader
          v-model="profession"
          :show-all-levels="false"
          :options="profConfigs"
          :props="{ expandTrigger: 'hover', label: 'value', value: 'id' }"
          placeholder="请选择专业"
        />
      </el-form-item>
      <el-form-item label="合同编号">
        <el-input v-model="achievementData.contractNo" placeholder="请填写合同编号" />
      </el-form-item>
      <el-form-item label="业务人员">
        <el-input v-model="achievementData.busiPerson" placeholder="请填写业务人员" />
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="achievementData.type" placeholder="请填写类型" />
      </el-form-item>
      <el-form-item label="收到日期">
        <el-date-picker
          v-model="achievementData.receiveDate"
          type="date"
          placeholder="请填写收到日期"
        />
      </el-form-item>
      <el-form-item label="来源">
        <el-input v-model="achievementData.fromType" placeholder="请填写来源" />
      </el-form-item>
      <el-form-item label="报告">
        <upload-multiple-file v-if="flag" :file-list="achievementData.reportFiles" @getFileList="getReportFileList" />
      </el-form-item>
      <el-form-item label="合同">
        <upload-multiple-file v-if="flag" :file-list="achievementData.contractFiles" @getFileList="getContractFileList" />
      </el-form-item>
      <el-form-item label="批复">
        <upload-multiple-file v-if="flag" :file-list="achievementData.replyFiles" @getFileList="getReplyFileList" />
      </el-form-item>
      <el-form-item label="成果确认单">
        <upload-multiple-file v-if="flag" :file-list="achievementData.confirmFiles" @getFileList="getConfirmFileList" />
      </el-form-item>
      <el-form-item label="归档">
        <el-input v-model="achievementData.archive" placeholder="请填写归档信息" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="achievementData.remark" type="textarea" placeholder="请填写备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="form-footer">
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button type="primary" @click="isEdit?updateAchievement():createAchievement()">提交</el-button>
    </div>
  </div>
</template>

<script>
import { getAchievement, addAchievement, editAchievement } from '@/api/achievement'
import UploadMultipleFile from '@/components/Upload/UploadMultipleFile.vue'
import { getBusiConfigList, getProfConfigTree } from '@/api/config'

export default {
  // import引入的组件需要注入到对象中
  components: {
    UploadMultipleFile
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    // 存放数据
    return {
      busiConfigs: [],
      profConfigs: [],
      profession: 0,
      achievementData: {},
      rules: {},
      reportFiles: null,
      flag: false
    }
  },
  // 监听属性
  computed: {
  },
  // 监控data中的数据变化
  watch: {
  },
  // 生命周期 - 创建完成
  created() {
    if (this.isEdit) {
      this.flag = false
      const id = this.$route.params.id
      this.fetchAchievement(id)
    } else {
      this.flag = true
    }
    getBusiConfigList().then(res => {
      this.busiConfigs = res.data
    })
    getProfConfigTree().then(res => {
      this.profConfigs = res.data
    })
  },
  // 方法
  methods: {
    async fetchAchievement(id) {
      await getAchievement(id).then(res => {
        this.achievementData = res.data
        this.profession = [this.achievementData.profId]
        console.log('###', this.achievementData.profParentId)
        if (this.achievementData.profParentId) {
          this.profession.unshift(this.achievementData.profParentId)
        }
        console.log(this.achievementData)
        console.log(this.profession)
        this.flag = true
      })
    },
    createAchievement() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.achievementData.profId = this.profession[1] || this.profession[0]
          console.log(this.achievementData)
          addAchievement(this.achievementData).then((response) => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateAchievement() {
      console.log(this.achievementData)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.achievementData)
          tempData.profId = this.profession[1] || this.profession[0]

          editAchievement(tempData).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    getReportFileList(files) {
      this.achievementData.reportFiles = files
    },
    getContractFileList(files) {
      this.achievementData.contractFiles = files
    },
    getReplyFileList(files) {
      this.achievementData.replyFiles = files
    },
    getConfirmFileList(files) {
      this.achievementData.confirmFiles = files
    }
  }
}
</script>
<style lang='scss' scoped>
// @import url(); // 引入公共css类
.main-wrapper {
  margin: 32px;
  width: 800px;
  .form-footer {
    text-align: center;
  }
}
</style>
